package com.bauer.springboot.apollo;

import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 主要有两种方式来获取
 * 1、API方式 很少使用  了解
 */
@SpringBootApplication
@Controller
@EnableConfigurationProperties
@EnableApolloConfig({"oss.properties"})
public class ApolloApplication {

    @ApolloConfig
    private Config connectionStr;

    @ApolloConfig("oss.properties")
    private Config oss;

    @Autowired
    private OssConfig ossConfig;

    @Bean
    public OssConfig getOssConfig(){
        return new OssConfig();
    }

    public static void main(String[] args) {
        SpringApplication.run(ApolloApplication.class, args);
    }

    @ResponseBody
    @RequestMapping("/hi")
    public String hi() {
        String result = connectionStr.getProperty("hello_apollo", "null");
        System.out.println(oss.getProperty("osskey", null));
        System.out.println(ossConfig.getAccessKey());
        return result;
    }

    @ApolloConfigChangeListener("appliction")
    public void applicationChanged(ConfigChangeEvent event){
        System.out.println(event.changedKeys());
    }

    @ApolloConfigChangeListener("oss.properties")
    public void ossChanged(ConfigChangeEvent event){
        System.out.println(event.changedKeys());
    }
}
